package com.zhan.ielts.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.ielts.dao.ListeningAnswerDao;
import com.zhan.ielts.domain.ListeningAnswer;

@Repository
public class ListeningAnswerDaoImpl extends BaseDaoImpl<ListeningAnswer> implements ListeningAnswerDao {

	@Override
	public List<ListeningAnswer> selectAnswerByCustom(ListeningAnswer entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectByCustom"),
				entity);
	}

	@Override
	public void updateByCustom(List<ListeningAnswer> entityList) {
		if (entityList == null || entityList.isEmpty())
			return;
		for (ListeningAnswer entity : entityList) {
			sqlSessionTemplate.update(getSqlName("updateByCustom"), entity);
		}
	}

	@Override
	public void saveOrUpdate(List<ListeningAnswer> entityList) {
		try {
			if (entityList != null && !entityList.isEmpty()) {
				for (ListeningAnswer entity : entityList) {
					if (StringUtils.isBlank(entity.getId()))
						entity.setId(generateId());
					sqlSessionTemplate.insert(getSqlName("saveOrUpdate"),
							entity);
				}
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s",
					getSqlName("saveOrUpdate")), e);
		}
	}

	@Override
	public List<ListeningAnswer> selectLatest(ListeningAnswer entity) {
		return sqlSessionTemplate.selectList(getSqlName("selectLatestOnes"), entity);
	}

	@Override
	public Long getCountByCutom(ListeningAnswer entity) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLatestCount"),entity);
	}

}
